<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <button type="button">download</button>
  <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
  <script>


    var getFile = url => {
      return new Promise((resolve, reject) => {
        $.ajax({
          method: 'get',
          url,
          responseType: 'arraybuffer'
        }).done(data => {
          resolve(data.data)
        }).fail(error => {
          reject(error.toString())
        })
      })
    }



    var downloadUrl = 'http://xxx.xx.cn/download/a.png'
    $('button').on('click', function() {
      // download(downloadUrl)
      // window.location.href = downloadUrl
      getFile(downloadUrl).then(data => {
        console.log(data)
      })
    })




    // 无闪现下载excel
    function download(url) {
      const iframe = document.createElement('iframe');
      iframe.style.display = 'none';
      function iframeLoad() {
        const win = iframe.contentWindow;
        const doc = win.document;
        if (win.location.href === url) {
          if (doc.body.childNodes.length > 0) {
            // response is error
          }
          iframe.parentNode.removeChild(iframe);
        }
      }
      if ('onload' in iframe) {
        iframe.onload = iframeLoad;
      } else if (iframe.attachEvent) {
        iframe.attachEvent('onload', iframeLoad);
      } else {
        iframe.onreadystatechange = function onreadystatechange() {
          if (iframe.readyState === 'complete') {
            iframeLoad;
          }
        };
      }
      iframe.src = '';
      document.body.appendChild(iframe);

      setTimeout(function loadUrl() {
        iframe.contentWindow.location.href = url;
      }, 50);
    }

  </script>
</body>
</html>
